package com.lxl.bt.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.google.gson.JsonObject;
import com.lxl.bt.Constant;
import com.lxl.bt.R;
import com.lxl.bt.bean.UserAccountBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.MyUtils;
import com.lxl.bt.util.Prefs;
import com.lxl.bt.util.ToastUtil;
import com.yanzhenjie.sofia.Sofia;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  提币
 * on  2021年4月8日22:37:20
 **/
public class TibiActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.etCode)
    EditText etCode;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.tvCommit)
    TextView tvCommit;
    @BindView(R.id.etAddress)
    EditText etAddress;
    @BindView(R.id.etNum)
    EditText etNum;
    @BindView(R.id.tvHint)
    TextView tvHint;
    @BindView(R.id.tvAccount)
    TextView tvAccount;
    @BindView(R.id.tvDaozhang)
    TextView tvDaozhang;
    @BindView(R.id.tvShouxu)
    TextView tvShouxu;
    @BindView(R.id.tv_all)
    TextView tvAll;

    private Context context;
    ACache aCache;
    UserAccountBean userAccountBean;
    private String[] titles = {"全部", "充值", "提现", "转账", "支付", "挖矿", "返佣"};
    private String[] activityTitles = {"USDT钱包", "BTC钱包", "ETH钱包", "LTC钱包", "FIL钱包", "电费钱包"};
    private String[] reqs = {"usdt", "btc", "eth", "ltc", "fil", ""};
    private int name;
    float shouxu = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tibil);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
//                .statusBarDarkFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);
        userAccountBean = (UserAccountBean) getIntent().getSerializableExtra("data");
        name = getIntent().getIntExtra("name", 0);
        context = this;
        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText("提币");
        rlRight.setVisibility(View.GONE);
        tvRight.setText("划转");
        aCache = ACache.get(this);
        tvHint.setText(String.format("仅支持转出%s钱包地址，转出后不可撤销，请谨慎交易", reqs[name]));
        switch (name) {
            case 0:
                tvAccount.setText(userAccountBean.getUsdtAmount() + reqs[name]);
                break;
            case 1:
                tvAccount.setText(userAccountBean.getBtcAmount() + reqs[name]);
                break;
            case 2:
                tvAccount.setText(userAccountBean.getEthAmount() + reqs[name]);
                break;
            case 3:
                tvAccount.setText(userAccountBean.getLtcAmount() + reqs[name]);
                break;
            case 4:
                tvAccount.setText(userAccountBean.getFilAmount() + reqs[name]);
                break;
            case 5:
                tvAccount.setText("0");
                break;
        }

        commissionByCoinName();
        limitWithdraw();

        etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(etNum.getText().toString())) {
                    tvDaozhang.setText("0" + reqs[name]);
                } else {
                    tvDaozhang.setText(Float.valueOf(etNum.getText().toString()) - shouxu + reqs[name]);
                }
            }
        });

    }

    private void commissionByCoinName() {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .commissionByCoinName(reqs[name]).
                enqueue(new NetCallback<Result<String>>() {
                    @Override
                    public void onSuccess(Result<String> result) {
                        Log.w("lxl", ">>>>" + result.isSuccess());
                        if (result.isSuccess()) {
                            tvShouxu.setText(result.getData() + reqs[name]);
                            shouxu = Float.parseFloat(result.getData());
                            tvDaozhang.setText("0" + reqs[name]);
                        } else {
                            ToastUtil.showToast(TibiActivity.this, result.getMsg());
                        }

                    }

                    @Override
                    public void netLogin() {

                    }

                    @Override
                    public void onError(String errorStr) {
                        super.onError(errorStr);
                    }

                });
    }

    private void limitWithdraw() {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .limitWithdraw(reqs[name]).
                enqueue(new NetCallback<Result<String>>() {
                    @Override
                    public void onSuccess(Result<String> result) {
                        Log.w("lxl", ">>>>" + result.isSuccess());
                        if (result.isSuccess()) {
                            etNum.setHint(String.format("最小提币数%s", result.getData()));
                        } else {
                            ToastUtil.showToast(TibiActivity.this, result.getMsg());
                        }

                    }

                    @Override
                    public void netLogin() {

                    }

                    @Override
                    public void onError(String errorStr) {
                        super.onError(errorStr);
                    }

                });
    }

    private void commit() {
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .cashwith(reqs[name], etCode.getText().toString(), etAddress.getText().toString(), etNum.getText().toString()).
                enqueue(new NetCallback<Result<String>>() {
                    @Override
                    public void onSuccess(Result<String> result) {
                        Log.w("lxl", ">>>>" + result.isSuccess());
                        if (result.isSuccess()) {
                            ToastUtil.showToast(TibiActivity.this, result.getMsg());
                            finish();
                        } else {
                            ToastUtil.showToast(TibiActivity.this, result.getMsg());
                        }

                    }

                    @Override
                    public void netLogin() {

                    }

                    @Override
                    public void onError(String errorStr) {
                        super.onError(errorStr);
                        Log.w("lxl", ">>>>" + errorStr);
                        ToastUtil.showToast(TibiActivity.this, errorStr);
                    }

                });
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            if (l > 0)
                tvGetCode.setText(l / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetCode.setClickable(true);
            tvGetCode.setText(getResources().getString(R.string.verification));
            tvGetCode.setTextColor(getResources().getColor(R.color.colorMain));
        }
    };

    private void getVerificationCode() {
        tvGetCode.setClickable(false);
        tvGetCode.setTextColor(getResources().getColor(R.color.c69));
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("phone", Prefs.getStringValue(Constant.PHONE));
        jsonObject.addProperty("macCode", 1);
        jsonObject.addProperty("type", 0);
        RetrofitUtil.getInstance().create(NetApi.class, 0)
                .getyCode(jsonObject).enqueue(new NetCallback<Result<String>>() {
            @Override
            public void onSuccess(Result<String> result) {
                Log.w("lxl", "onsuccess>>>>");
                timer.start();

            }

            @Override
            public void netLogin() {
                Log.w("lxl", "netLogis>>>>");
            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                Log.w("lxl", "error>>>>");
                tvGetCode.setClickable(true);
                tvGetCode.setText(getResources().getString(R.string.verification));
                tvGetCode.setTextColor(getResources().getColor(R.color.colorMain));
                MyUtils.shortToastMsg(context, errorStr);
            }

        });
    }

    @OnClick({R.id.rlBack, R.id.tvGetCode, R.id.tvCommit,R.id.tv_all})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
            case R.id.tvGetCode:
                getVerificationCode();
                break;
            case R.id.tvCommit:
                commit();
                break;
                case R.id.tv_all:
                    String amount = tvAccount.getText().toString().replace(reqs[name], "");
                    etNum.setText(amount);
                    break;
        }
    }
}
